from django.db import models
from django.conf import settings


# 文章标签
class Tag(models.Model):
    name = models.CharField('文章标签', max_length=20)

    description = models.TextField(default="", verbose_name="标签描述")

    class Meta:
        verbose_name = '标签'
        verbose_name_plural = verbose_name
        ordering = ['id']
        db_table = "knowledge_tag"

    def __str__(self):
        return self.name


# 文章表
class Article(models.Model):
    author = models.CharField(max_length=100, verbose_name='作者名字', default="佚名")
    title = models.CharField(max_length=150, verbose_name='文章标题')
    summary = models.TextField(verbose_name='文章摘要', max_length=230, default='默认的文章摘要...')
    body = models.TextField(verbose_name='文章内容', default="")
    create_date = models.DateTimeField(verbose_name='创建时间', auto_now_add=True)
    update_date = models.DateTimeField(verbose_name='修改时间', auto_now=True)

    # tags = models.ManyToManyField(Tag, verbose_name='标签', db_table="knowledge_tag_to_article")

    class Meta:
        verbose_name = '文章'
        verbose_name_plural = verbose_name
        ordering = ['-create_date']
        db_table = "knowledge_article"

    def __str__(self):
        return self.title[:20]

    # def get_absolute_url(self):
    #     return reverse('blog:detail', kwargs={'slug': self.slug})
    #
    # def body_to_markdown(self):
    #     return markdown.markdown(self.body, extensions=[
    #         'markdown.extensions.extra',
    #         'markdown.extensions.codehilite',
    #     ])

    def get_pre(self):
        return Article.objects.filter(id__lt=self.id).order_by('-id').first()

    def get_next(self):
        return Article.objects.filter(id__gt=self.id).order_by('id').first()


# 文章标签表
class ArticleToTag(models.Model):
    article = models.ForeignKey(to=Article, on_delete=models.CASCADE)
    tag = models.ForeignKey(to=Tag, on_delete=models.CASCADE)

    create_date = models.DateTimeField(verbose_name='创建时间', auto_now_add=True)

    class Meta:
        verbose_name = '文章标签表'
        verbose_name_plural = verbose_name
        ordering = ['-create_date']
        db_table = "knowledge_article_to_tag"

    def __str__(self):
        return "《{}》: {}".format(self.article.title, self.tag.name)

    @classmethod
    def get_articles_by_tag_pk(cls, tag_pk):
        res = []
        relation_set = cls.objects.filter(tag_id=tag_pk)
        for r in relation_set:
            res.append({
                "pk": r.article.id,
                "fields": {
                    "titile": r.article.title
                }

            })
        return res


# 概念表/专业术语表
class Concept(models.Model):
    # 概念名
    name = models.CharField(max_length=100, verbose_name="术语名称")

    introduce = models.TextField(verbose_name="术语介绍", default="暂无")

    class Meta:
        verbose_name = '术语表'
        verbose_name_plural = verbose_name
        db_table = "knowledge_concept"

    def __str__(self):
        return self.name


# 知识点表
class Point(models.Model):
    # 知识点内容
    context = models.TextField(default="默认知识点内容...", verbose_name="知识点内容")

    # 所属文章
    article = models.ForeignKey(Article, verbose_name="所属文章", on_delete=models.CASCADE)

    # 主语
    subject = models.ForeignKey(to=Concept, related_name="subject", verbose_name="主语对应的术语概念", on_delete=models.SET_NULL,
                                null=True)

    # 宾语
    object = models.ForeignKey(to=Concept, related_name="object", verbose_name="宾语对应的术语概念", on_delete=models.SET_NULL,
                               null=True)

    class Meta:
        verbose_name = '知识点表'
        verbose_name_plural = verbose_name
        db_table = "knowledge_point"

    def __str__(self):
        return self.context
#
# # 知识点-概念表
# class PointToConcept(models.Model):
#     # 知识点
#     point = models.ForeignKey(to=Point, verbose_name="所属知识点", on_delete=models.CASCADE)
#
#     # 主语
#     subject = models.ForeignKey(to=Concept, related_name="subject", verbose_name="主语对应的术语概念", on_delete=models.SET_NULL,
#                                 null=True)
#
#     # 宾语
#     object = models.ForeignKey(to=Concept, related_name="object", verbose_name="宾语对应的术语概念", on_delete=models.SET_NULL,
#                                null=True)
#
#     class Meta:
#         verbose_name = '知识点-术语对应表'
#         verbose_name_plural = verbose_name
#         db_table = "knowledge_point_to_concept"

# # 文章分类
# class Category(models.Model):
#     name = models.CharField('文章分类', max_length=20)
#     slug = models.SlugField(unique=True)
#     description = models.TextField('描述', max_length=240, default=settings.SITE_DESCRIPTION,
#                                    help_text='用来作为SEO中description,长度参考SEO标准')
#
#     class Meta:
#         verbose_name = '分类'
#         verbose_name_plural = verbose_name
#         ordering = ['name']
#
#     def __str__(self):
#         return self.name
#
#     def get_article_list(self):
#         return Article.objects.filter(category=self)
